<?php
/**
 * Created by PhpStorm.
 * User: 王飞
 * Date: 2017/8/24
 * Time: 0:22
 */

namespace app\admin\controller\staff;

use app\admin\model\CommentRankValue;
use app\admin\model\Order as OrderModel;
use app\common\controller\Backend;
use app\common\model\Category as CategoryModel;
use app\admin\model\Comment as Commentmodel;
use think\Session;

/**
 * Class Order
 * @package app\admin\controller\staff
 * 此类为我的评论控制器
 */
class Comment extends Backend
{
	protected $model = null;
	
	public function _initialize()
	{
		parent::_initialize();
		$this->model = model('comment');
		if(isset($_GET['staffid']))$this->view->assign('staffid',$_GET['staffid']);
	}
	/**
	 * 此函数为显示为员工的评论列表
	 */
	public function index()
	{
		if ($this->request->isAjax()) {
			list($where, $sort, $order, $offset, $limit) = $this->buildparams(NULL);
			$total = $this->model->with('worklog')->with('user')
				->where($where)->where('staff_id', $_GET['staffid'])
				->order($sort, $order)
				->count();
			$list = $this->model->with('worklog')->with('user')
				->where($where)->where('staff_id', $_GET['staffid'])
				->order($sort, $order)
				->limit($offset, $limit)
				->select();
			
			foreach ($list as $key => $value){
				if($value['worklog_id']!=null){$list[$key]['username']=$value['user']['username'];}else{$list[$key]['username']='管理员添加的用户：'.$value['username'];}
			}
			
			$result = array("total" => $total, "rows" => $list);
			return json($result);
		}
		$this->view->assign('staffid', input('get.userid'));
		return $this->view->fetch();
	}
	public function add()
	{
		$rankstypelist=db('comment_ranks_type')->select();
		$this->view->assign('ranktypelist',$rankstypelist);
		
		if($this->request->isPost()){
			$this->code = 1;
			$this->msg ="";
			$comment=new Commentmodel;
			$data['context']=$_POST['row']['context'];
			$data['createtime']=$_POST['row']['createtime'];
			$data['staff_id']=$_POST['row']['staff_id'];
			$data['weight']=$_POST['row']['weight'];
			$comment->save($data);
			
			$imgarr=explode(",",$_POST['images']);
			$imgsdata=[];
			foreach ($imgarr as $key=>$value){
				$item=['comment_id'=>$comment->id,'pic_src'=>$value];
				array_push($imgsdata,$item);
			}
			db('comment_pic')->insertAll($imgsdata);
			
			$ranklist=$_POST['rank'];
			$rankdata=[];
			foreach ($ranklist as $key=>$value){
				$item=['comment_id'=>$comment->id,'comment_rank_type_id'=>$key,'value'=>$value];
				array_push($rankdata,$item);
			}
			$commentrankvalue=new CommentRankValue();
			if($commentrankvalue->where('comment_id',$comment->id)->find()!=null){
				foreach ($rankdata as $value){
					$result=$commentrankvalue->save($value);
					if ($result === false)
					{
						$this->msg = $this->model->getError();
					}
				}
			}
			else{
				$result=$commentrankvalue->saveAll($rankdata);
				if ($result === false)
				{
					$this->msg = $this->model->getError();
				}
			}
			
			return;
		}
		return $this->view->fetch();
	}
	public function edit($ids=null)
	{
		$id=$ids;
		$commentlist=db('comment')->where('id',$id)->find();
		$rankstypelist=db('comment_ranks_type')->select();
		$ranksvaluelist=db('comment_rank_value')->where('comment_id',$id)->select();
		foreach ($ranksvaluelist as $value){
			$rankvalue[$value['comment_rank_type_id']]=$value['value'];
		}
		$imglist=db('comment_pic')->where('comment_id',$id)->select();
		$imgsrcstr="";
		foreach ($imglist as $value){
			$imgsrcstr.=$value['pic_src'].",";
		}
		$imgsrcstr=substr($imgsrcstr,0,strlen($imgsrcstr)-1);
		$this->view->assign('commentlist',$commentlist);
		$this->view->assign('imgsrcstr',$imgsrcstr);
		$this->view->assign('rankvalue',$rankvalue);
		$this->view->assign('ranktypelist',$rankstypelist);
		
		if($this->request->isPost()){
			$this->code = 1;
			$this->msg ="";
			$comment=new Commentmodel;
			$data['context']=$_POST['row']['context'];
			$data['createtime']=$_POST['row']['createtime'];
			$data['weight']=$_POST['row']['weight'];
			$comment->save($data,['id' => $id]);
			
			$imgarr=explode(",",$_POST['images']);
			$imgsdata=[];
			db('comment_pic')->where('comment_id',$id)->delete();
			foreach ($imgarr as $key=>$value){
				$item=['comment_id'=>$id,'pic_src'=>$value];
				array_push($imgsdata,$item);
			}
			db('comment_pic')->insertAll($imgsdata);
			
			$ranklist=$_POST['rank'];
			$rankdata=[];
			foreach ($ranklist as $key=>$value){
				$item=['comment_id'=>$id,'comment_rank_type_id'=>$key,'value'=>$value];
				array_push($rankdata,$item);
			}
			$commentrankvalue=new CommentRankValue();
			if($commentrankvalue->where('comment_id',$id)->find()!=null){
				foreach ($rankdata as $value){
					$result=$commentrankvalue->update($value);
					if ($result === false)
					{
						$this->msg = $this->model->getError();
					}
				}
			}
			else{
				$result=$commentrankvalue->saveAll($rankdata);
				if ($result === false)
				{
					$this->msg = $this->model->getError();
				}
			}
			
			return;
		}
		return $this->view->fetch();
	}
	
}
	